// ============================================================ //
// ============================================================ //
//
// @Group       : QiXin Supply Chain Team
// @Author      : ZhiQiang Cheng
// @Date        : 2024-07-15
// @Description : 团购商品队列
//
// ============================================================ //
// ============================================================ //

package dao

import (
	"wooha-cache-hub/model"
	"wooha-cache-hub/utils"

	"github.com/StarfishProgram/starfish-go-sdk/sdkdb"
	"github.com/StarfishProgram/starfish-go-sdk/sdktypes"
)

type groupBuyingRoomProductQueue struct{}

var GroupBuyingRoomProductQueue = new(groupBuyingRoomProductQueue)

// ============================================================ //
// 查询当前和等待开启的队列
// ============================================================ //

func (gbrpq *groupBuyingRoomProductQueue) GetInProgressById(roomId sdktypes.ID) ([]*model.GroupBuyingRoomProductQueueFull, error) {
	var result []*model.GroupBuyingRoomProductQueueFull
	sql := sdkdb.Ins().Table("group_buying_room_product_queue rpq")
	sql.Joins("left join group_buying gb on gb.id = rpq.group_buying_id")
	sql.Joins("left join group_buying_room gbr on rpq.room_id = gbr.id")
	sql.Joins("left join product p on p.id = rpq.product_id")
	sql.Joins("left join brand b on b.id = p.brand_id")
	sql.Joins("left join sku s on s.id = rpq.sku_id")
	sql.Where("rpq.room_id = ? and rpq.id >= gbr.queue_id", roomId)
	sql.Order("rpq.id asc")
	sql.Select(`
		rpq.*,
		b.name as brand_name,
		p.name as product_name,
		s.specificationValues as specification_values,
		p.productImages as img,
		gb.progress as progress,
		gb.status as group_buying_status,
		gb.begin_date as begin_date
	`)
	if err := sql.Find(&result).Error; err != nil {
		return nil, err
	}
	for _, item := range result {
		item.Img = utils.ConvertImageUrl(item.Img)
	}
	return result, nil
}
